package com.app.shop.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.app.shop.config.security.DgbSecurityUserHelper;
import com.app.shop.controller.system.BaseController;
import com.app.shop.entity.WgPicture;
import com.app.shop.entity.WgPicture;
import com.app.shop.entity.query.WgPictureQuery;
import com.app.shop.entity.show.BaseShow;
import com.app.shop.entity.show.WgPictureShow;
import com.app.shop.service.shop.WgPictureService;
import com.app.shop.util.ApiRest;
import com.app.shop.util.ClassUtil;
import com.app.shop.util.Constant;
import com.app.shop.util.SysModel;
import com.github.pagehelper.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 微信文章页面
 *
 * @author chen li wei
 * @version 21:14 2019-5-10
 */
@Controller
@RequestMapping(value = "/wgPicture")
public class WgPictureController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(WgPictureController.class);

    @Autowired
    WgPictureService wgPictureService;

    /**
     * 查询图片资源列表
     * @param wgPictureQuery 查询商品参数
     * @return 返回商品列表
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping(value = "/getPictureList")
    public ApiRest getDictList(@RequestBody WgPictureQuery wgPictureQuery) {
        log.debug(JSONObject.toJSON(wgPictureQuery).toString());
        //查询
        Page<WgPicture> wgPictureList=wgPictureService.getWgPictureList(wgPictureQuery);
        //查询结果对象置换
        List<WgPictureShow> resList=new ArrayList<WgPictureShow>();
        WgPictureShow wgPictureShow;
        //数据转换
        for(WgPicture wgPicture:wgPictureList){
            wgPictureShow=new WgPictureShow();
            try {
                ClassUtil.fatherToChild(wgPicture,wgPictureShow);
            } catch (Exception e) {
                e.printStackTrace();
            }
            //码表值补全
            wgPictureShow.dataSet();
            resList.add(wgPictureShow);
        }
        return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE,
                new BaseShow(wgPictureList.getPageNum(), wgPictureList.getPageSize(), wgPictureList.getTotal(), wgPictureList.getPages(), resList));    }

    @ResponseBody
    @RequestMapping(value = "/getPictureAllList")
    public ApiRest getPictureAllList(@RequestBody WgPictureQuery wgPictureQuery) {
        log.debug(JSONObject.toJSON(wgPictureQuery).toString());
        //查询
        Page<WgPicture> wgPictureList=wgPictureService.getWgPictureList(wgPictureQuery);
        //查询结果对象置换
        List<WgPictureShow> resList=new ArrayList<WgPictureShow>();
        WgPictureShow wgPictureShow;
        //数据转换
        for(WgPicture wgPicture:wgPictureList){
            wgPictureShow=new WgPictureShow();
            try {
                ClassUtil.fatherToChild(wgPicture,wgPictureShow);
            } catch (Exception e) {
                e.printStackTrace();
            }
            //码表值补全
            wgPictureShow.dataSet();
            resList.add(wgPictureShow);
        }
        return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE,
                new BaseShow(wgPictureList.getPageNum(), wgPictureList.getPageSize(), wgPictureList.getTotal(), wgPictureList.getPages(), resList));    }



    @ResponseBody
    @RequestMapping(value = "editPictureDelete")
    public ApiRest editPictureDelete(@RequestBody WgPicture wgPicture) {
        log.debug(JSONObject.toJSON(wgPicture).toString());
        wgPicture.setModifiedUserId(DgbSecurityUserHelper.getUserInfo().getUserId());
        wgPicture.setModifiedDate(new Date());
        SysModel sysModel = wgPictureService.editPictureDelete(wgPicture);
        if (sysModel.isSuccess()) {
            return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE, null);
        } else {
            return new ApiRest(Constant.REST_FAIL_CODE, sysModel.getMsg(), null);
        }
    }
}
